﻿using System;
using System.Web;
using gms.data;

namespace gms.security {
	public static class Authentication {
		public static int authed_player {
			get {
				if (session_key_exists("player_id")) {
					return int.Parse(HttpContext.Current.Session["player_id"].ToString());
				}
				else {
					return -1;
				}
			}
			set {
				HttpContext.Current.Session["player_id"] = value;
			}
		}
		public static string authed_ip {
			get {
				return HttpContext.Current.Request.UserHostAddress;
			}
		}
		public static bool is_session_authed() {
			bool retval = false;
			//Player login = new Player();

			//if (authed_player != -1) {
			//    login.load_by_id(authed_player);

			//    if (login.address.Equals(authed_ip)) {
			//        retval = true;
			//    }
			//    else {
			//        clear_session();
			//    }
			//}
			//else {
			//    clear_session();
			//}

			return retval;
		}
		public static bool check_login(string mail, string pass) {
			bool retval = true;
			//player login = new player();
			//byte[] login_pass;

			//// does user exist
			//login.load_by_email(mail);

			//if (login.player_id == -1) {
			//    retval = false;
			//}

			//if (retval) {
			//    login_pass = password.get_password_hash(pass, login.salt);

			//    string output = "";
			//    for (int i = 0; i < login_pass.Length; i++) {
			//        output += login_pass[i].ToString();
			//    }

			//    if (!password.bytes_are_same(login_pass, login.password)) {
			//        retval = false;
			//    }
			//}

			return retval;
		}
		public static void clear_session() {
			HttpContext.Current.Session.Clear();
		}
		private static bool session_key_exists(string key) {
			bool retval = false;

			if (HttpContext.Current.Session != null) {
				foreach (string local_key in HttpContext.Current.Session.Keys) {
					if (local_key.Equals(key)) {
						retval = true;
					}
				}
			}

			return retval;
		}
	}
}